import {Injectable, Inject} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import { stringify } from 'query-string';

import {API_CONFIG, ServicesModule} from './services.module';
import {Singer, SingerParams, defaultSingerParams} from '../data-types/common.types';

@Injectable({
  providedIn: ServicesModule
})
export class SingerService {

  constructor(
    private http: HttpClient,
    @Inject(API_CONFIG) private uri: string
  ) {
  }

  getEnterSinger(args: SingerParams = defaultSingerParams): Observable<Array<Singer>> {
    const params = new HttpParams({
      fromString: stringify(args)
    });
    return this.http.get(`${this.uri}/artist/list`, { params }).pipe(
      map((res: { artists: Array<Singer> }) => res.artists)
    );
  }

}
